#coding=utf-8
#测试用例标题：两次账号密码登录
#测试场景：分别测试账号密码正确和不正确的情况
#创建者：陈强
#创建日期：2017/5/10
#最后修改日期：2017/5/10
#输入参数：描述脚本的输入参数
#输出参数：描述脚本的输出参数
#路径：E:\\PYcode\\Test\\result
#coding=utf-8
from selenium import webdriver
import unittest
import HTMLTestRunner
from time import sleep
import datetime,time
import os

class P2PTest(unittest.TestCase):
    #P2P登录测试用例
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()   #最大化窗口
        self.driver.implicitly_wait(8)  #隐形等待8S
        self.base_url = u"https://www.yidaip2p.net/user/login/"
        self.driver.get(self.base_url)
        os.chdir('D:\python-photo\log')  #切换到当前目录
        report = open("P2P-login.doc", "a")  #打开报告
        current_time = datetime.datetime.now()
        report.write("\n%s:"%current_time)
        report.write("打开前端页面成功\n")

    def P2Plogins(self, username, pwd):
        driver = self.driver
        #登录方法,账号密码参数化
        driver.find_element_by_id("keywords").clear()  #清除输入框内容
        driver.find_element_by_id("keywords").send_keys(username)   #输入账号
        driver.find_element_by_id("password").clear()  #清除输入框内容
        driver.find_element_by_id("password").send_keys(pwd)      #输入密码
        driver.find_element_by_id("login_button").click()  #点击登录按钮
        sleep(3)

    def is_login_sucess(self):
        driver = self.driver
        #判断是否获取到登录账号名称
        try:
            text = driver.find_element_by_css_selector(".login.clearfix>strong").text
            print text
            return True
        except:
            print u"账号或者密码不匹配"
            driver.quit()
            return False

    def test_01(self):
        self.P2Plogins(u"17781108472", u"jk123123")
        os.chdir('D:\python-photo\log')  # 切换到当前目录
        report = open("P2P-login.doc", "a")  # 打开报告
        current_time = datetime.datetime.now()
        report.write("%s:" % current_time)
        report.write("登陆成功\n")
        now = time.strftime("%Y-%m-%M-%H_%M_%S", time.localtime(time.time()))  #截图命名格式
        pic_path = 'D:\\python-photo\\image\\'+now+'login0'+'.png'  #截图保存路径
        print pic_path
        self.driver.save_screenshot(pic_path)   #截图
        result = self.is_login_sucess()
        self.assertTrue(result)



    def test_02(self):
        self.P2Plogins(u"17782208473", u"jk123123")
        os.chdir('D:\python-photo\log')  # 切换到当前目录
        report = open("P2P-login.doc", "a")  # 打开报告
        current_time = datetime.datetime.now()
        report.write("%s:" % current_time)
        report.write("登陆失败\n")
        now = time.strftime("%Y-%m-%M-%H_%M_%S", time.localtime(time.time()))  #截图命名格式
        pic_path = 'D:\\python-photo\\image\\'+now+'login1'+'.png'  #截图保存路径
        print pic_path
        self.driver.save_screenshot(pic_path)   #截图
        result = self.is_login_sucess()
        self.assertFalse(result)


    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    testunit = unittest.TestSuite()
    testunit.addTest(P2PTest("test_01"))  #记录test01
    testunit.addTest(P2PTest("test_02"))  #记录test02
    #定义输出报告路径
    now = time.strftime("%Y-%m-%M-%H_%M_%S", time.localtime(time.time()))
    htmlfile = 'D:\\python-photo\\html\\'+now+'P2Plogin0'+'.html'
    print htmlfile
    fp = file(htmlfile, 'wb')  #输出报告，html格式
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"测试报告", description=u"用例测试报告")
    runner.run(testunit)
    fp.close()



